require("../less/courseDetails.less")

document.ready(function () {
    //获取dom
    let back = document.querySelector("#back");//返回
    let startBtn = document.querySelector("#startBtn");//开始训练
    let videoDom = document.querySelector("#videos");//视频
    let videos = document.querySelector("#videos");//视频
    
    videos.addEventListener("click",function(){
        window.location.href = "./player.html?cid=" + cid
    })

    // console.log("课程详情...");
    // 获取到传递过来的课程ID
    let cid = window.location.search.split("=")[1]
    console.log("传递过来的课程ID:", cid);
    // 发请求去获取当前课程的详情
    axios.get(window.$utils.BASE_URL + "/sports/courseDetail?id=" + cid)
        .then((res) => {
            let result = res.data;
             console.log("22",result);
            if (result.status === 0) {
                //1 把视频信息存放在本地存储中,以供播放页面使用
                window.localStorage.setItem("videos", window.JSON.stringify(result.data.fragments));
                // console.log("eee",result.data.fragments);
                //获取新的预览视频
                let arr = result.data;
                console.log("444",arr);
                //找视频预览 
                let videos = arr.find(function(v){
                    return v.latest === 3
                });
                console.log("p",videos);
                //渲染视频预览
                showcourDetails(videos);
               
            }
        })
        .catch((err) => {
            console.log(err);
        })
   //渲染视频预览
//    showcourDetails();
   function showcourDetails(videos){
       /* console.log("渲染视频",video); */
      if(videos){
        let str =`"/sports/courseDetail?id="
        <a  class="course-link mt10" href="courseDetails.html?courseid=${videos.courseId}">
        <div class="video " id="videos">
        <img src="${window.$utils.BASE_URL + videos.imgurl}">
    </div>
        `
        // console.log("rrr",videos.videoUrl);
        videoDom.innerHTML = str;

      }
   }
    /* 返回按钮 */
    back.addEventListener("click", function () {
        window.history.go(-1)
    })
    /* 开始训练按钮 */
    startBtn.addEventListener("click", function () {
        // alert(666)
        // 跳转到播放页面
        window.location.href = "./player.html?cid=" + cid
    })
})